package com.hualan.sfzyproject.controller;


import com.hualan.sfzyproject.domain.PsyTrueReply;
import com.hualan.sfzyproject.service.PsyTrueReplyService;
import com.hualan.sfzyproject.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/psyTrueReply")
public class PsyTrueReplyController {
    @Autowired
    private PsyTrueReplyService psyTrueReplyService;

    @GetMapping("/getByQuestionId")
    public R getByQuestionId(int questionId)
    {
        List<PsyTrueReply> psyTrueReplies = psyTrueReplyService.selectByQuestionId(questionId);
        if (psyTrueReplies != null){
            return R.ok(psyTrueReplies);
        }
        return R.error("获取失败");
    }


    @PostMapping("/insert")
    public R insert(@RequestBody PsyTrueReply psyTrueReply)
    {
        System.out.println(psyTrueReply);
        int i ;
        if(psyTrueReply.getFatherReplyId() != null){
             i = psyTrueReplyService.insert(psyTrueReply.getReplyContent(), psyTrueReply.getUserId(), psyTrueReply.getQuestionId(), psyTrueReply.getFatherReplyId());
        }else {
             i = psyTrueReplyService.insert(psyTrueReply.getReplyContent(), psyTrueReply.getUserId(), psyTrueReply.getQuestionId());
        }

        if (i > 0){
            return R.ok("回复成功");
        }
        return R.error("回复失败");
    }

    @GetMapping("/getByUserId")
    public R getByUserId(int userId, int pageNum, int pageSize)
    {
        List<PsyTrueReply> psyTrueReplies = psyTrueReplyService.selectByUserId(userId, pageNum, pageSize);
        if (psyTrueReplies != null){
            return R.ok(psyTrueReplies);
        }
        return R.error("获取失败");
    }

    //模糊搜索
    @GetMapping("/selectByLike")
    public R selectByLike(String like) {
        List<PsyTrueReply> psyTrueReplies = psyTrueReplyService.selectByLike(like);
        if (psyTrueReplies != null){
            return R.ok(psyTrueReplies);
        }
        return R.error("获取失败");
    }
    @DeleteMapping("/deleteByReplyId/{replyId}")
    public R deleteByReplyId(@PathVariable("replyId") int replyId)
    {
        int i = psyTrueReplyService.deleteByReplyId(replyId);
        if (i > 0){
            return R.ok("删除成功");
        }
        return R.error("删除失败");
    }
    @DeleteMapping("/deleteByQuestionId/{questionId}")
    public R deleteByQuestionId(@PathVariable("questionId") int questionId)
    {
        int i = psyTrueReplyService.deleteByQuestionId(questionId);
        if (i > 0){
            return R.ok("删除成功");
        }
        return R.error("删除失败");
    }

    @GetMapping("/getAll")
    public R getAll()
    {
        List<PsyTrueReply> psyTrueReplies = psyTrueReplyService.getAll();
        if (psyTrueReplies != null){
            return R.ok(psyTrueReplies);
        }
        return R.error("获取失败");
    }

}
